package com.wlyuan.open.infrastructure.repository;

import com.wlyuan.mybatis.utils.RepositoryUtils;
import com.wlyuan.open.dao.entity.OpenApiLogDO;
import com.wlyuan.open.dao.mapper.OpenApiLogMapper;
import com.wlyuan.open.domain.api.OpenApiLog;
import com.wlyuan.open.domain.api.respository.OpenApiLogRepository;
import com.wlyuan.open.domain.api.valueobj.OpenApiLogId;
import com.wlyuan.open.infrastructure.converter.OpenApiLogConverter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author yuanzheng
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OpenApiLogRepositoryImpl implements OpenApiLogRepository {
    private final OpenApiLogMapper openApiLogMapper;

    @Override
    public OpenApiLog ofId(@NotNull OpenApiLogId openApiLogId) {
        return OpenApiLogConverter.convert(openApiLogMapper.selectById(openApiLogId.getId()));
    }

    @Override
    public OpenApiLogId save(OpenApiLog openApiLog) {
        var apiLogDO = RepositoryUtils.save(openApiLogMapper,
                OpenApiLogConverter::convert,
                openApiLog, openApiLog.getId());

        openApiLog.setId(new OpenApiLogId(apiLogDO.getId()));
        return openApiLog.getId();
    }

    @Override
    public void remove(OpenApiLogId openApiLogId) {
        openApiLogMapper.deleteById(openApiLogId.getId());
    }

    @Override
    public List<OpenApiLog> list(Collection<OpenApiLogId> openApiLogIds) {
        if (CollectionUtils.isEmpty(openApiLogIds)) {
            return Collections.emptyList();
        }
        return RepositoryUtils.list(openApiLogMapper,
                OpenApiLogConverter::convert,
                OpenApiLogDO::getId, openApiLogIds);
    }
}
